using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Comteck.Winforms.Forms;

namespace Comteck.Winforms.Dialog {
  public partial class FrmWaitDialog : Form {
    /// <summary>
    /// 注释格式化字符串
    /// </summary>
    private string descFormat = "共{0}/{1}，{2}";

    public FrmWaitDialog() {
      InitializeComponent();
      this.StartPosition = FormStartPosition.CenterParent;
      this.descFormat = this.ckDescription.Text;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="parent"></param>
    public FrmWaitDialog(Form parent) : this() {
      if (parent == null) {
        this.StartPosition = FormStartPosition.CenterParent;

        // 按钮不可见
        this.ckCancel.Visible = false;
      } else {
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(parent.Location.X + parent.Width / 2 - this.Width / 2, parent.Location.Y + parent.Height / 2 - this.Height / 2);
      }

      this.StartPosition = FormStartPosition.CenterScreen;

      // 记录这个窗口的父窗体
      this.Tag = parent;
    }
    /// <summary>
    /// 关闭窗口，释放资源
    /// </summary>
    public void CloseForm() {
      //try {
      //  if (this.ckLoadImage.Image != null) {
      //    this.ckLoadImage.Image.Dispose();
      //  }
      //} catch (Exception ex) {
      //  Global.logger.Error(ex.Message, ex);
      //}

      this.DialogResult = DialogResult.OK;
      this.Close();
    }

    /// <summary>
    /// 设置提示信息
    /// </summary>
    /// <param name="current">当前第几个</param>
    /// <param name="count">总记录数</param>
    /// <param name="orgCode">组织编码</param>
    /// <param name="orgName">组织名称</param>
    public void SetDescription(int current, int count, string orgCode, string orgName) {
      try {
        this.ckDescription.Text = String.Format(this.descFormat, current, count, $"{orgCode} {orgName}");
      } catch (Exception ex) {
        //Global.logger.Error(ex.Message, ex);
      }
    }

    /// <summary>
    /// 设置提示信息
    /// </summary>
    /// <param name="desc">描述</param>
    public void SetDescription(string desc) {
      try {
        this.ckDescription.Text = desc;
      } catch (Exception ex) {
        //Global.logger.Error(ex.Message, ex);
      }
    }

    private void FrmWaitDialog_Load(object sender, EventArgs e) {
      if (this.DesignMode) return;
    }

    private void ckCancel_Click(object sender, EventArgs e) {
      if (this.Tag == null) return;

      // 客户中断查询
      var form = this.Tag as FrmBase;
      if (form != null) {
        form.IsExecuting = false;

      }

      this.ckCancel.Text = "正在取消中";
      this.ckCancel.Enabled = false;
    }




    /// <summary>
    /// 查询等待框(按获取第三方账单获取) 控制类
    /// </summary>
    public class WaitByForms {
      /// <summary>
      /// 加载框
      /// </summary>
      private FrmWaitDialog loadingForm;
      /// <summary>
      /// 当前线程
      /// </summary>
      private Thread loadthread;
      /// <summary>
      /// 标题信息
      /// </summary>
      public string Caption { get; set; }

      /// <summary>
      /// 显示等待框
      /// </summary>
      /// <param name="parent">父窗体</param>
      public void Show(Form parent = null) {
        this.loadthread = new Thread(new ParameterizedThreadStart(LoadingProcessEx));
        this.loadthread.Start(parent);
      }

      /// <summary>
      /// 关闭等待框
      /// </summary>
      public void Close() {
        if (this.loadingForm == null) return;



        // 修改状态
        var form = this.loadingForm.Tag as FrmBase;
        if (form != null) {
          form.IsExecuting = true;
        }

        // 异步关闭窗口
        loadingForm.BeginInvoke(new System.Threading.ThreadStart(loadingForm.CloseForm));
        loadingForm = null;
        loadthread = null;
      }

      /// <summary>
      /// 异步打开加载窗口
      /// </summary>
      /// <param name="parent"></param>
      private void LoadingProcessEx(object parent = null) {
        loadingForm = new FrmWaitDialog(parent as Form);
        if (!string.IsNullOrWhiteSpace(this.Caption))
          loadingForm.ckCaption.Text = this.Caption;
        loadingForm.ShowDialog();
      }

      /// <summary>
      /// 设置提示信息
      /// </summary>
      /// <param name="current">当前第几个</param>
      /// <param name="count">总记录数</param>
      /// <param name="orgCode">组织编码</param>
      /// <param name="orgName">组织名称</param>
      public void SetDescription(int current, int count, string orgCode, string orgName) {
        while (true) {
          if (this.loadthread != null && this.loadthread.ThreadState == ThreadState.Running
            && this.loadingForm != null && this.loadingForm.IsHandleCreated) {
            this.loadingForm.BeginInvoke((MethodInvoker)delegate
            {
              // modify by geping@2017-12-15,此处必须加“?”进行null判断，否则在第一条数据执行前取消会报错“未找到对象”
              this.loadingForm?.SetDescription(current, count, orgCode, orgName);
            });
            break;
          }
        }
      }

      /// <summary>
      /// 设置提示信息
      /// </summary>
      /// <param name="desc">描述</param>
      public void SetDescription(string desc) {
        while (true) {
          if (this.loadthread != null && this.loadthread.ThreadState == ThreadState.Running
            && this.loadingForm != null && this.loadingForm.IsHandleCreated) {
            this.loadingForm.BeginInvoke((MethodInvoker)delegate
            {
              this.loadingForm?.SetDescription(desc);
            });
            break;
          }
        }
      }
    }








  }
}
